﻿// ---------------------------------------------------------------------
// Lớp PythonEngine
//
// Nội dung:
//    + Môi trường thực thi script Python
//
// Copyright (c) 2009 YugiOH
//
// Nguyễn Hữu Lê Trọng Tín      YM: ilu2009             ĐT: 0906520611 
// Võ Phước Hậu                 YM: phuochauspkt        ĐT: 0979202707
// ---------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IronPython.Compiler;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using Microsoft.Scripting;

namespace ManagementEffect.Engine
{
    public class PythonEngine
    {
        ScriptEngine engine;
        ScriptRuntime runtime;
        ScriptScope scope;
        CompiledCode code;

        public PythonEngine()
        {
            engine = Python.CreateEngine();
            runtime = engine.Runtime;
            scope = engine.CreateScope();
        }
        public PythonEngine(string source)
        {
            engine = Python.CreateEngine();
            runtime = engine.Runtime;
            scope = engine.CreateScope();
            Compile(source);
        }
        public PythonEngine(System.IO.FileInfo file)
        {
            engine = Python.CreateEngine();
            runtime = engine.Runtime;
            scope = engine.CreateScope();
            Compile_File(file.FullName);
        }
        public void SetCode(string source)
        {
            Compile(source);
        }
        private void Compile(string source)
        {
            ScriptSource script = engine.CreateScriptSourceFromString(source, SourceCodeKind.Statements);
            code = script.Compile();
        }
        private void Compile_File(string source)
        {
            ScriptSource script = engine.CreateScriptSourceFromFile(source);
            code = script.Compile();
        }
        public void SetVariable(string[] name,params object[] obj)
        {
            for (int i = 0; i < name.Length; i++)
            {
                scope.SetVariable(name[i], obj[i]);
            }                     
        }
        public object GetVariable(string name)
        {
            return scope.GetVariable(name);
        }
        public void SetAssembly(System.Reflection.Assembly ass)
        {
            runtime.LoadAssembly(ass);
        }
        public void Excute()
        {
            runtime.IO.RedirectToConsole();
            code.Execute(scope);
        }

        public static bool TestCompile(string source ,out string error)
        {

            error = "";
            try
            {
                ScriptEngine engine = Python.CreateEngine();
                ScriptSource script = engine.CreateScriptSourceFromString(source, SourceCodeKind.Statements);
                script.Compile();                
            }
            catch (SyntaxErrorException ex)
            {
                error = ex.Message + "   ("+ex.Line + " : " +ex.Column+")";
                return false;
            }
            return true;
        }
    }
}
